package net.scdx.hotel.controller;

import net.scdx.hotel.dto.AdminExecution;
import net.scdx.hotel.entity.Admin;
import net.scdx.hotel.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public String adminLogin(HttpServletRequest request, HttpServletResponse response) {
//        Map<String,Object> modelMap = new HashMap<>();
        try {
            // get request params
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            AdminExecution adminExecution = adminService.adminLogin(username, password);

            if (adminExecution.isSuccess() == false) {
                request.setAttribute("loginFailure", true);
            } else {
                // login successfully
                request.getSession().setAttribute("admin", adminExecution.getAdmin());
            }

            //request.getRequestDispatcher("/").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return "admin";
    }
}
